<!--点名情况-->
<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="12">
        <el-button
          type="primary"
          plain
          @click="handleAdd"
          v-hasPermi="['registrar:class:add']"
        >
          添加学员
        </el-button>
      </el-col>
      <el-col :span="11" style="display: flex; flex-direction: row-reverse">
        <el-form-item prop="name">
          <el-input
            v-model="outerQueryParams.name"
            placeholder="搜索学员姓名/手机号"
            clearable
            style="width: 240px"
            @keyup.enter="getList"
          >
            <template #append>
              <el-button :icon="Search" @click="getList" />
            </template>
          </el-input>
        </el-form-item>
      </el-col>
      <right-toolbar :search="false" @queryTable="getList"> </right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="outerList" border>
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gender">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.gender" />
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="消耗方式" align="center" prop="courseName" />
      <el-table-column label="剩余额度" align="center" prop="num">
        <template #default="scope">{{ scope.row.num + "课时" }}</template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleTransfer(scope.row)"
            v-hasPermi="['registrar:class:adjust']"
          >
            调至其他班
          </el-button>
          <el-button
            link
            type="primary"
            @click="handleRemove(scope.row)"
            v-hasPermi="['registrar:class:adjust']"
          >
            移除本班
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <MemberSelector
      ref="mSelector"
      v-bind="$props"
      @refreshPage="getList"
      from="default"
      title="添加学员"
    />

    <el-dialog
      v-model="dialog2Visible"
      title="调至其他班"
      width="500px"
      :before-close="handleDialog2Close"
    >
      <div class="content">
        <el-form
          :model="transferForm"
          ref="transferRef"
          :inline="true"
          label-width="88px"
        >
          <el-form-item label="目标班级" prop="classId">
            <el-select
              v-model="transferForm.classId"
              placeholder="请选择目标班级"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in otherClassList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleDialog2Close">取消</el-button>
          <el-button type="primary" @click="handleSubmit2"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { getMumberListAPI, removeMumberAPI } from "@/api/registrar/class";

import { getClassListAPI } from "@/api/registrar/class";
import { Search } from "@element-plus/icons-vue";

import MemberSelector from "./memberSelector.vue";

const props = defineProps({
  // 关联课程id,多个用逗号分隔
  courseFilterIds: {
    type: String,
  },
  classId: {
    type: String,
  },
  isVip: {
    type: String,
  },
});

// 变量--------------------------------------------------------------------------------------------------------------------
const { proxy } = getCurrentInstance();
const { sys_user_sex } = proxy.useDict("sys_user_sex"); // 0男 1女 2未知
const loading = ref(false);
const dialog2Visible = ref(false);
const mSelector = ref(null);

const data = reactive({
  outerQueryParams: {
    name: "",
  },
  // 调至其他班form
  transferForm: {
    classId: null,
  },
});

const { outerQueryParams, transferForm } = toRefs(data);

const outerList = ref([]);
const otherClassList = ref([]); // 刨除当前班级的其他班级列表
const current = ref({}); // 当前学员，用于调至其他班和移除班级等操作

const emits = defineEmits(["refreshPage"]);

// 事件-------------------------------------------------------------------------------------------------------------------

function handleAdd() {
  mSelector.value.openSelector();
}

function handleDialog2Close() {
  dialog2Visible.value = false;
}

function handleTransfer(row) {
  getClassList(row);
  dialog2Visible.value = true;
  current.value = row;
  resetTransferForm();
}

function handleRemove(row) {
  proxy.$modal
    .confirm('是否确认将"' + row.name + '"从本班移除？')
    .then(function () {
      return removeMumberAPI({
        id: row.id,
        studentId: row.studentId,
        classId: props.classId,
      });
    })
    .then(() => {
      proxy.$modal.msgSuccess("移除成功");
      getList();
    })
    .catch(() => {});
}

function resetTransferForm() {
  transferForm.value.classId = null;
}

// 调至其他班
function handleSubmit2() {
  loading.value = true;

  removeMumberAPI({
    id: current.value.id,
    studentId: current.value.studentId,
    classId: props.classId,
    classIdNew: transferForm.value.classId,
  })
    .then((response) => {
      if (response.msg && response.msg === "E102")
        proxy.$modal.msgError("调班失败，该学员已存在于要移至的班级里");
      else proxy.$modal.msgSuccess("调班成功");
      dialog2Visible.value = false;
      getList();
    })
    .catch(() => {
      loading.value = false;
    });
}

// 方法--------------------------------------------------------------------------------------------------------------------
// 班级
function getClassList(row) {
  loading.value = true;
  getClassListAPI({
    pageSize: 9999,
    pageNum: 1,
    isVip: props.isVip,
    courseId: row.courseId,
    beOver: "1",
    isRepair: 1,
  }).then((response) => {
    otherClassList.value = response.rows.filter((c) => c.id !== props.classId);
    loading.value = false;
  });
}

function getList() {
  loading.value = true;
  getMumberListAPI({
    ...outerQueryParams.value,
    classId: props.classId,
    pageSize: 9999,
    pageNum: 1,
  }).then((response) => {
    outerList.value = response.rows;
    loading.value = false;
    refreshParent(outerList.value.length); // 父组件的人数更新
  });
}

function refreshParent(num) {
  emits("refreshPage", num);
}

// 调用-----------------------------------------------------------------------------------------

getList();
</script>

<style lang="scss">
.content .pagination-container .el-pagination {
  right: 20px !important;
}
</style>
